package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.ClientUtils;
import com.ruoyi.common.OrderNumUtil;
import com.ruoyi.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
@Slf4j
@Component
public class ShankHandUtils {

    @Autowired
    MqttLockUtil mqttLockUtil;

    public void checkStatus(String deviceNum) {
        log.info("握手开始");
        int msgId = Integer.parseInt(OrderNumUtil.getRandom());
        log.info(msgId+"");
        //加锁asd
        mqttLockUtil.lock(msgId + "");
        try {
            JSONObject jsonObject = ClientUtils.doGetMethod("http://localhost:8610/check?deviceNum=" + deviceNum + "&msgId=" + msgId);
            log.info(jsonObject.toJSONString());
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException("设备无响应,请稍后再试!");
        }
        if (!mqttLockUtil.checkLock(msgId + "", 3)) {
            throw new ServiceException("设备无响应!");
        }
    }
}
